/*
 * GL-Shw锟斤拷锟杰筹拷锟斤拷位锟斤拷  qq群:297809802 qq1849057843
 *
 *目录锟斤拷
 *|||||||||||||||||||锟斤拷锟斤拷锟斤拷图锟斤拷协锟斤拷只锟斤拷选锟斤拷一锟街★拷
 * 一锟斤拷锟揭讹拷图锟斤拷
 *  锟斤拷锟斤拷通锟揭讹拷图锟斤拷
 *  锟节匡拷锟斤拷锟脚灰讹拷图锟斤拷
 *  锟斤拷压锟斤拷锟揭讹拷图锟斤拷
 * 锟斤拷锟斤拷锟斤拷值锟斤拷图锟斤拷锟斤拷*锟斤拷协锟斤拷锟斤拷锟�40锟斤拷锟斤拷锟揭碉拷压锟斤拷锟绞猴拷校锟斤拷 锟斤拷[强锟斤拷锟狡硷拷]
 *  锟劫讹拷值锟斤拷图锟斤拷
 *  锟节达拷校锟斤拷锟街碉拷锟酵硷拷锟�
 *  锟斤拷锟睫革拷图锟斤拷锟叫★拷亩锟街碉拷锟酵硷拷锟�
 *
 * ||||||||||||||||||锟斤拷锟斤拷锟斤拷协锟斤拷锟斤拷锟斤拷锟酵硷拷锟叫拷椴拷锟绞癸拷谩锟�
 * 锟斤拷锟斤拷图锟斤拷锟竭界（寻锟竭的边斤拷锟斤拷息锟斤拷锟狡硷拷锟斤拷锟斤拷锟街低硷拷锟斤拷锟绞凳憋拷榭达拷锟斤拷锟斤拷锟窖帮拷卟锟斤拷撸锟絒锟狡硷拷]
 * 锟侥★拷锟斤拷锟捷憋拷签锟斤拷锟斤拷锟斤拷示锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷色锟疥定锟斤拷锟斤拷锟捷硷拷锟接ｏ拷锟角憋拷锟教ｏ拷[强锟斤拷锟狡硷拷]
 *                          锟斤拷锟斤拷时锟戒：2022锟斤拷12锟斤拷9锟斤拷
 */
#include "zf_common_headfile.h"

// 发送一个字节
#define  sw_write_byte(dat)  uart_write_byte(UART_2, dat)
// 发送一串数据
#define  sw_write_buffer(dat,len)    uart_write_buffer(UART_2, dat, len)

// 灰度图传
void sendimg (uint8 *image, uint8 width, uint8 height)
{
    uint8 dat[6] = {0x21, 0x7A, width, height, 0x21, 0x7A};
    sw_write_buffer(dat, 6);
    system_delay_ms(1);
    sw_write_buffer(image, width * height);
    system_delay_ms(10);
}

void sendimg_A (uint8 *image, uint8 width, uint8 height)
{

    sw_write_byte(0x21);
    sw_write_byte(0x7A);
    sw_write_byte(width);
    sw_write_byte(height);
    sw_write_byte((width + height) / 2);
    sw_write_byte(0x7A);

    uint8 line = 0, col = 0;
    for (line = 0; line < width; line++)
    {
        sw_write_byte(21);
        sw_write_byte(line);
        sw_write_byte(133);
        for (col = 0; col < height; col++)
        {
            sw_write_byte(*(image + line * height + col));

        }

    }
}

// 发送压缩图像
void sendimg_zoom (uint8 *image, uint8 width, uint8 height, uint8 dis_width, uint8 dis_height)
{
    uint8 dat[6] = {0x21, 0x7A, dis_width, dis_height, 0x21, 0x7A};
    sw_write_buffer(dat, 6);
    uint8 i, j;
    for (j = 0; j < dis_height; j++)
    {
        for (i = 0; i < dis_width; i++)
        {
            sw_write_byte(*(image + (j * height / dis_height) * width + i * width / dis_width)); //锟斤拷取锟斤拷锟截碉拷

        }
    }
}

// 二值化图传
void sendimg_binary (uint8 *image, uint8 width, uint8 height, uint8 otu)
{

    uint8 dat[6] = {0x7A, 0x21, width, height, 0x7A, 0x21};
    sw_write_buffer(dat, 6);
    int databool = 255;
    uint8 lon = 1;
    int data = 255;
    uint8 line = 0, col = 0;

    for (line = 0; line < width; line++)
    {
        for (col = 0; col < height; col++)
        {
            if (*(image + line * height + col) > otu)
                data = 255;
            else
                data = 0;
            if (data == databool)
            {
                lon++;
            }
            else
            {
                sw_write_byte(lon);
                lon = 1;
            }
            if (lon == 190)
            {
                sw_write_byte(lon - 1);
                sw_write_byte(0);
                lon = 1;
            }
            databool = data;
        }
    }
}

// 发送灰度指针图
void sendimg_binary_vec (uint8** image, uint8 width, uint8 height, uint8 otu)
{

    uint8 dat[6] = {0x7A, 0x21, width, height, 0x7A, 0x21};
    sw_write_buffer(dat, 6);
    int databool = 255;
    uint8 lon = 1;
    int data = 255;
    uint8 line = 0, col = 0;

    for (line = 0; line < width; line++)
    {
        for (col = 0; col < height; col++)
        {
            if (*image[line * height + col] > otu)
                data = 255;
            else
                data = 0;
            if (data == databool)
            {
                lon++;
            }
            else
            {
                sw_write_byte(lon);
                lon = 1;
            }
            if (lon == 190)
            {
                sw_write_byte(lon - 1);
                sw_write_byte(0);
                lon = 1;
            }
            databool = data;
        }
    }
}

// 压缩二值化图传
void sendimg_binary_zoom (uint8 *image, uint8 width, uint8 height, uint8 dis_width, uint8 dis_height, uint8 otu)
{

    uint8 dat[6] = {0x7A, 0x21, dis_width, dis_height, 0x7A, 0x21};
    sw_write_buffer(dat, 6);
    int databool = 255;
    uint8 lon = 1;
    int data = 255;
    uint8 i, j;
    for (j = 0; j < dis_height; j++)
    {
        for (i = 0; i < dis_width; i++)
        {
            if (*(image + (j * height / dis_height) * width + i * width / dis_width) > otu) //锟斤拷取锟斤拷锟截碉拷
                data = 255;
            else
                data = 0;
            if (data == databool)
            {
                lon++;
            }
            else
            {
                sw_write_byte(lon);
                lon = 1;
            }
            if (lon == 190)
            {
                sw_write_byte(lon - 1);
                sw_write_byte(0);
                lon = 1;
            }
            databool = data;
        }
    }

}

// 带有校验的二值化图传
void sendimg_binary_CHK (uint8 *image, uint8 width, uint8 height, uint8 otu, uint8 chk)
{
    chk = chk > 0 ? chk : 0;
    chk = chk < 56 ? chk : 55;
    uint8 dat[7] = {0x7A, 0x21, width, height, 0x7A, 0x21, 200 + chk};
    sw_write_buffer(dat, 7);
    int databool = 255;
    uint8 lon = 0;
    int data = 255;
    uint8 line = 0, col = 0;
    int imglon = 0;
    int imgdatlo = width * height / chk;
    uint8 CHK = 0;
    for (line = 0; line < width; line++)
    {
        for (col = 0; col < height; col++)
        {
            imglon++;

            if (*(image + line * height + col) > otu)
                data = 255;
            else
                data = 0;
            if (data == databool)
            {
                lon++;
            }
            else
            {
                sw_write_byte(lon);
                lon = 1;
            }

            if (imglon == imgdatlo)
            {
                CHK++;
                sw_write_byte(lon);
                data = 255;
                databool = 255;
                sw_write_byte(200 + CHK);
                lon = 0;
                imglon = 0;
            }
            if (lon == 190)
            {
                sw_write_byte(lon);
                sw_write_byte(0);
                lon = 0;
            }
            databool = data;
        }
    }
}

// 显示数据及其数据名称(int)
void put_int32 (uint8 name, int dat)
{
    uint8 datc[10] = {197, name, 1, 0, 0, 0, 0, 0, 0, 198};
    datc[3] = (uint8) (dat & 0xFF);
    datc[4] = (uint8) ((dat & 0xFF00) >> 8);
    datc[5] = (uint8) ((dat & 0xFF0000) >> 16);
    datc[6] = (uint8) ((dat >> 24) & 0xFF);
    uint8 crc[6] = {name, 1, datc[3], datc[4], datc[5], datc[6]};
    uint16 CRC16 = swj_CRC(crc, 0, 6);
    datc[7] = (uint8) (CRC16 & 0xff);
    datc[8] = (uint8) (CRC16 >> 8);
    sw_write_buffer(datc, 10);
}

// 显示数据及其数据名称(float)
void put_float (uint8 name, float dat)
{
    uint8 datc[10] = {197, name, 2, 0, 0, 0, 0, 0, 0, 198};
    char farray[4] = {0};

    *(float*) farray = dat;
    unsigned char *p = (unsigned char*) &dat + 3;
    datc[3] = *(p - 3);
    datc[4] = *(p - 2);
    datc[5] = *(p - 1);
    datc[6] = *p;

    uint8 crc[6] = {name, 2, datc[3], datc[4], datc[5], datc[6]};
    uint16 CRC16 = swj_CRC(crc, 0, 6);
    datc[7] = (uint8) (CRC16 & 0xff);
    datc[8] = (uint8) (CRC16 >> 8);
    sw_write_buffer(datc, 10);
}

// 清屏
void sendline_clear (uint8 color, uint8 width, uint8 height)
{
    sw_write_byte(0x21);
    sw_write_byte(0x7A);
    sw_write_byte(width);
    sw_write_byte(height);
    sw_write_byte(color);
    sw_write_byte(0x21);
}

// 发送线的坐标
void sendline_xy (uint8 *line_zx, uint8 *line_yx, uint8 len)
{
    sw_write_byte(0x21);
    sw_write_byte(9);
    sw_write_byte(len);
    sw_write_byte(255);
    sw_write_byte(255);
    sw_write_buffer(line_zx, len);
    sw_write_buffer(line_yx, len);
}

// 发送线(uint8)
void sendline_uint8 (uint8 color, uint8 *buff, uint8 len)
{
    sw_write_byte(0x21);
    sw_write_byte(color);
    sw_write_byte(len);
    sw_write_byte(255);
    sw_write_byte(255);
    sw_write_buffer(buff, len);
}

// 发送线(float)
void sendline_float (uint8 color, float (*buff)[2], uint8 len)
{
    sw_write_byte(0x21);
    sw_write_byte(color);
    sw_write_byte(len);
    sw_write_byte(254);
    sw_write_byte(255);
    for (uint32 i = 0; i < len; ++i)
        sw_write_byte((uint8)buff[i][0]);
    for (uint32 i = 0; i < len; ++i)
        sw_write_byte((uint8)buff[i][1]);
}

// 发送线,x,y坐标分开传入
void sendline2 (uint8 color, uint8 *linex, uint8 *liney, uint8 len)
{
    sw_write_byte(0x21);
    sw_write_byte(color);
    sw_write_byte(len);
    sw_write_byte(254);
    sw_write_byte(255);
    sw_write_buffer(linex, len);
    sw_write_buffer(liney, len);

}

// 画一个点
void sendpoint (uint8 color, uint8 x, uint8 y, uint8 type)
{
    sw_write_byte(0x22);
    sw_write_byte(color);
    sw_write_byte(type);
    sw_write_byte(254);
    sw_write_byte(x);
    sw_write_byte(y);
    sw_write_byte(255);
}

// crc校验
uint16 swj_CRC (uint8 *Buf, uint8 CRC_sta, uint8 CRC_CNT)
{
    uint16 CRC_Temp;
    uint8 i, j;
    CRC_Temp = 0xffff;

    for (i = CRC_sta; i < CRC_CNT; i++)
    {
        CRC_Temp ^= Buf[i];
        for (j = 0; j < 8; j++)
        {
            if (CRC_Temp & 0x01)
                CRC_Temp = (CRC_Temp >> 1) ^ 0xa001;
            else
                CRC_Temp = CRC_Temp >> 1;
        }
    }
    return (CRC_Temp);
}
//int32 ByteToInt(int8 *ndsk)
//{
//    int32 m = 0;
//    int8 *h = (int8 *)&m;
//    h[0] = ndsk[0];
//    h[1] = ndsk[1];
//    h[2] = ndsk[2];
//    h[3] = ndsk[3];
//
//    if(0 == m)
//    {
//        m = 1;
//    }
//
//    return m;
//}
//float ByteToFloat(unsigned char* byteArry)
//{
//return *((float*)byteArry);
//}



// 匿名上位机调试
void ano_send_flexible_frame (int8 flag, int16 value)
{
    // 固定2字节头部
    uint8 datc[8] = { 0xAA, 0xAF, flag, 2, 0, 0, 0, 0 };
    datc[4] = (uint8) (value & 0xFF);
    datc[5] = (uint8) ((value & 0xFF00) >> 8);

    for(uint8 i = 0; i < 6; i++)
    {
        datc[6] += datc[i];
        datc[7] += datc[6];
    }
    sw_write_buffer(datc, 8);
}

//--------------------------------------------------锟斤拷锟斤拷模锟斤拷-----------------------------------------------//
/*
 * 锟斤拷锟斤拷模锟斤拷 锟狡硷拷使锟斤拷锟斤拷傻锟斤拷锟斤拷锟侥ｏ拷椋╱sb to nrf锟斤拷*锟斤拷锟斤拷时锟斤拷锟斤拷同时锟斤拷锟斤拷ttl锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷小锟斤拷锟斤拷锟斤拷锟斤拷模锟斤拷
 * 锟斤拷锟斤拷锟绞匡拷锟斤拷锟斤拷锟斤拷锟斤拷锟�460800 *锟斤拷应小锟斤拷锟较的筹拷锟斤拷,小锟斤拷锟斤拷锟斤拷锟斤拷模锟介，锟斤拷锟斤拷锟较碉拷锟斤拷锟斤拷模锟介，锟斤拷位锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷
 * 锟斤拷锟斤拷模锟斤拷锟劫度憋拷wifi锟斤拷 锟斤拷锟脚碉拷锟斤拷锟饺讹拷 锟斤拷锟节灰讹拷图锟斤拷锟劫度诧拷太锟斤拷锟矫ｏ拷锟斤拷锟叫讹拷值锟斤拷图锟斤拷锟脚伙拷 锟绞合憋拷图锟斤拷锟斤拷示锟斤拷锟斤拷
 *
 */
